package com.songlanyun.modules.singlegoods.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.modules.singlegoods.entity.ProductSingleGoodsEntity;
import com.songlanyun.modules.singlegoods.model.UnderStockRecordDTO;
import com.songlanyun.modules.singlegoods.service.ProductSingleGoodsService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.singlegoods.dao.UnderStockRecordDao;
import com.songlanyun.modules.singlegoods.entity.UnderStockRecordEntity;
import com.songlanyun.modules.singlegoods.service.UnderStockRecordService;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;


@Service("understockRecordService")
public class UnderStockRecordServiceImpl extends ServiceImpl<UnderStockRecordDao, UnderStockRecordEntity> implements UnderStockRecordService {

    @Resource
    private ProductSingleGoodsService productSingleGoodsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long shopId = MapUtil.getLong(params, "shop_id");

        IPage<UnderStockRecordEntity> page = this.baseMapper.recordPage(new Query<UnderStockRecordEntity>().getPage(params), params);

//        IPage<UnderStockRecordEntity> page = this.page(
//                new Query<UnderStockRecordEntity>().getPage(params),
//                new LambdaQueryWrapper<UnderStockRecordEntity>()
//                        .eq(UnderStockRecordEntity::getIsInStock, false)
//                        .eq(shopId != null, UnderStockRecordEntity::getShopId, shopId)
//                        .orderByDesc(UnderStockRecordEntity::getCreateTime)
//        );

        return new PageUtils(page);
    }


    @Override
    public void add(UnderStockRecordDTO dto) {
        //单品
        ProductSingleGoodsEntity singleGoods = productSingleGoodsService.loadByShopIdAndId(dto.getShopId(), dto.getSingleGoodsId());

        UnderStockRecordEntity recordEntity = loadBySingleGoodsId(dto.getSingleGoodsId());
        if (recordEntity == null) {
            //不存在记录
            recordEntity = new UnderStockRecordEntity();
        }
        BeanUtil.copyPropertiesIgnoreNull(dto, recordEntity);
        recordEntity.setIsInStock(false);
        recordEntity.setSingleGoodsNum(singleGoods.getAbleStock());
        this.saveOrUpdate(recordEntity);
    }

    private UnderStockRecordEntity loadBySingleGoodsId(Long singleGoodsId) {
        return this.baseMapper.selectOne(
                new LambdaQueryWrapper<UnderStockRecordEntity>()
                        .eq(UnderStockRecordEntity::getIsInStock, false)
                        .eq(UnderStockRecordEntity::getSingleGoodsId, singleGoodsId)
                        .last("limit 1")
        );

    }

    @Override
    public void inStock(Long singleGoodsId) {
        this.update(
                new LambdaUpdateWrapper<UnderStockRecordEntity>()
                        .eq(UnderStockRecordEntity::getSingleGoodsId, singleGoodsId)
                        .eq(UnderStockRecordEntity::getIsInStock, false)
                        .set(UnderStockRecordEntity::getIsInStock, true)
        );


    }

}